package com.excesys.exsecs.scada.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.excesys.exsecs.common.file.core.FileProperties;
import com.excesys.exsecs.scada.entity.ScadaEntity;
import com.excesys.exsecs.scada.entity.SysDictItem;
import com.excesys.exsecs.scada.mapper.SysDictItemCopyMapper;
import com.excesys.exsecs.scada.properties.PathProperties;
import com.excesys.exsecs.scada.service.ScadaPropertyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 修改JSON文件
 */
@Component
@AllArgsConstructor
public class JsonProcess {


	private ScadaEntity scadaEntity;
	private SysDictItemCopyMapper sysDictItemMapper;

	private ScadaPropertyService scadaPropertyService;
	private final FileProperties properties;
	
	public void ChangeJson(String path,String fileName){
		//从字典中取参数
		SysDictItem address = sysDictItemMapper.selectOne(Wrappers.<SysDictItem>lambdaQuery()
				.eq(SysDictItem::getDictType, "scada_address"));
		String scadaAddress = address.getItemValue();
		if(ObjectUtils.isEmpty(address) || StringUtils.isEmpty(scadaAddress)){
			scadaAddress = "127.0.0.1:8080";
		}
		SysDictItem http = sysDictItemMapper.selectOne(Wrappers.<SysDictItem>lambdaQuery()
				.eq(SysDictItem::getDictType, "scada_http"));
		String scadaHttp = http.getItemValue();
		if(ObjectUtils.isEmpty(http) || StringUtils.isEmpty(scadaHttp)){
			scadaHttp = "http://";
		}
		File[] ls = FileUtil.ls(path);
		List<String> fileNames = new ArrayList<>();
		for (int i = 0; i < ls.length; i++) {
			fileNames.add(ls[i].getName());
		}
		if(!fileNames.contains("png") && !fileNames.contains("file")){
			return;
		}
		String json = FileUtil.readUtf8String(path + "\\json\\2d.json");
		if(!StringUtils.isNotEmpty(json)){
			System.out.println("********读不到文件2d.json*******");
		}
		JSONObject jsonObject = new JSONObject(json);
		List<Map<String,Object>> penList  = (List<Map<String, Object>>) jsonObject.get("pens");
		for (int i = 0; i < penList.size(); i++) {
			String image = (String) penList.get(i).get("image");
			System.out.println("origin image:"+ image);
			if(StringUtils.isNotEmpty(image)){
				int index = image.lastIndexOf("public");
				System.out.println("index:"+index);
				if(index == -1){
					penList.get(i).replace("image",scadaHttp + scadaAddress+ PathProperties.PROXY+ fileName + PathProperties.SUFFIX + image);
				}else {
					image = image.substring(index+6);
					System.out.println("new image:" + image);
					penList.get(i).replace("image",scadaHttp + scadaAddress + PathProperties.PROXY+ fileName + PathProperties.SUFFIX + image);
				}

			}
		}
		jsonObject.replace("pens",penList);
		List<Map<String,Object>> newpens =(List<Map<String, Object>>) jsonObject.get("pens");
		for (int i = 0; i < newpens.size(); i++) {
			String newimage = (String)newpens.get(i).get("image");
			if(StringUtils.isNotEmpty(newimage)){
				System.out.println(newimage);
			}
		}
		//写文件
		FileUtil.writeString(jsonObject.toString(),path+"\\json\\2d.json", Charset.forName("utf8"));

	}

	//修改所有JSON文件
	public void ChangeAllJson(){
		//查询数据库组态配置列表
		List<String> fileNames = scadaPropertyService.list().stream().map(scadaProperty -> {
			String[] split = scadaProperty.getFileName().split("\\.");
			return split[0];
		}).collect(Collectors.toList());

		//遍历、读文件、写文件
		for (int i = 0; i < fileNames.size(); i++) {
			String packagePath = properties.getLocal().getBasePath()+ FileUtil.FILE_SEPARATOR
					+ "package" + FileUtil.FILE_SEPARATOR+ fileNames.get(i);
			String filePath = packagePath + FileUtil.FILE_SEPARATOR + "meta2d-vue2" + FileUtil.FILE_SEPARATOR + "public";
			if(!FileUtil.exist(packagePath)){
				continue;
			}
			ChangeJson(filePath,fileNames.get(i));
		}

	}


	public void ChangeHttp(String path, String scadaId){
		//
		String json = FileUtil.readUtf8String(path);
		if(!StringUtils.isNotEmpty(json)){
			System.out.println("********读不到文件2d.json*******");
		}
		JSONObject jsonObject = new JSONObject(json);
		List<JSONObject> https = (List<JSONObject>) jsonObject.get("https");
		if(ObjectUtils.isNotEmpty(https)){
			https.get(0).replace("http", "http://"+scadaEntity.getAddress()+":9998/admin/scadaModel/data?id="+scadaId);
			jsonObject.set("https",https);
		} else{
			JSONObject jsonmap = new JSONObject();
			jsonmap.set("http","http://"+scadaEntity.getAddress()+":9998/admin/scadaModel/data?id="+scadaId);
			jsonmap.set("httpTimeInterval",1000);
			jsonmap.set("httpHeaders","{}");
			System.out.println(jsonmap);
			List httpList = new ArrayList<>();
			httpList.add(jsonmap);
			jsonObject.set("https",httpList);
		}

		//写文件
		FileUtil.writeString(jsonObject.toString(),path,Charset.forName("utf8"));

	}



	
}
